package net.ruangtedy.java.spring.formhandling.web.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.mvc.Controller;
import org.springframework.web.servlet.mvc.ParameterizableViewController;
import org.springframework.web.servlet.view.JstlView;
import org.springframework.web.servlet.view.UrlBasedViewResolver;

@Configuration
@ComponentScan(basePackages={"net.ruangtedy.java.spring.formhandling.web"})
@EnableWebMvc
public class WebMvcCtxConfig {
	/**
	 * Cara ini digunakan untuk view Controller, memetakan request yang masuk (index.htm) dan kemudian setViewName
	 * menuju jsp untuk dirender
	 * @return
	 */
	@Bean(name="/index.htm")//<-- request yang masuk
	public Controller index(){
		ParameterizableViewController index;
		index=new ParameterizableViewController();
		index.setViewName("index");//--> JSP yang akan dirender
		return index;
	}

	/**
	 * Setting View Resolver disini
	 * @return
	 */
	@Bean
	public UrlBasedViewResolver setupViewResolver() {
		UrlBasedViewResolver resolver = new UrlBasedViewResolver();
		resolver.setPrefix("/WEB-INF/views/");
		resolver.setSuffix(".jsp");
		resolver.setViewClass(JstlView.class);
		return resolver;
	}

}
